grammar ParserGrammer;

options {
  language = Java;
}

@header {
  package database.queryparser;
}

@lexer::header {
  package database.queryparser;
}

statement 
      :( ('CREATE' | 'create' | 'Create')  ('table' | 'TABLE') IDENT 
      '(' 
       (IDENT ('INTEGER' | 'integer' | 'Integer') ('Primary Key' | 'primary key' | 'PRIMARY KEY')* ',')*
       (IDENT ('INTEGER' | 'integer' | 'Integer') ('Primary Key' | 'primary key')*)
      ')' ';'
      ) 
      
      |
      
      ( ('CREATE' | 'create' | 'Create') ('index' | 'INDEX' | 'Index') 
      IDENT ('on' | 'ON' |'On' | 'oN') IDENT '(' IDENT ')' ';' )
      
      |
      
      ( ('insert' | 'Insert' | 'INSERT') ('into' | 'INTO' |'Into') IDENT ('values' | 'VALUES' | 'Values') 
      '(' INTEGER ((',') INTEGER)* ')' ';'      
       ) 
       
        |
        
       ( ('drop' | 'DROP' | 'Drop') ('index' | 'INDEX' | 'Index' | 'table' | 'TABLE' | 'Table') IDENT) ';'
       
       |       
              
       ( ('SELECT' | 'select' | 'Select') ('*' | (IDENT '.' IDENT ',')*(IDENT ',')* ((IDENT '.' IDENT) | (IDENT)) ) 
       ('from' | 'FROM' | 'From') (IDENT ',')* (IDENT)
       (('where' | 'WHERE' | 'Where') (expression))* 
        (('order by' | 'ORDER BY' | 'Order By' | 'Order by') (expression) (('desc' | 'DESC' | 'Desc' | 'ASC' |'asc' | 'Asc')
        (('LIMIT' | 'Limit' | 'limit') INTEGER )*)*)* ';'
       )       
      ;
      
//expression
statement1
        : assignment
        ;
      
assignment
         : IDENT '=' expression
         ;
                 


term 
    : IDENT
    | '(' expression ')'
    | INTEGER
    ;

negation
        : 'not'? term
        ;
      
unary
    : ('+' | '-')* negation
    ;
    
mult
    : unary (('*' | '/' | 'mod') unary)*
    ;
    
add
   : mult (('+' | '-') mult)*
   ;
   
relation 
        : add (('=' | '!=' | '<' | '<=' | '>' | '>=') add)*
        ;                   
 
expression
         : relation (('and' | 'AND' | 'or' | 'OR' | '&&' |'||') relation)*
         ; 
    
IDENT : ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9')*;
INTEGER : ('0'..'9')('0'..'9')*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN ;};


